package com.jh3502.vivo.function.travel;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jh3502.vivo.R;

import java.util.ArrayList;
import java.util.Map;

import butterknife.ButterKnife;

public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> implements View.OnClickListener, View.OnLongClickListener {


    private Context mContext;
    private ArrayList<Map<String, Object>> listItems;
    //    private String batInfoTemp;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null;
    private ButtonInterface viewClickTest, viewClickConfig, viewClickDelete, viewClickView;

    public TravelListAdapter(Context context, ArrayList<Map<String, Object>> datas) {
        this.listItems = datas;
        this.mContext = context;
    }

    public void setButtonSetOnclickTest(ButtonInterface buttonInterface) {
        this.viewClickTest = buttonInterface;
    }

    public void setButtonSetOnclickConfig(ButtonInterface buttonInterface) {
        this.viewClickConfig = buttonInterface;
    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }

    public void setButtonSetOnclickView(ButtonInterface buttonInterface) {
        this.viewClickView = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_todo, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        convertView.setOnLongClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
//        String batInfo = SpHelper.getInstance().getBatAct();
//        viewHolder.itemView.setTag(position + "");
//        viewHolder.itemHome1Num.setText(position + 1 + "");
//
//        if (position % 2 != 0) {
//            viewHolder.itemHome1.setBackgroundColor(ContextCompat.getColor(mContext, R.color.gray_bg));
//        } else {
//            viewHolder.itemHome1.setBackgroundColor(Color.WHITE);
//        }
//
//
//        String batno = (String) listItems.get(position).get("batno");
//        viewHolder.itemHome1Id.setText(batno);
//
//        viewHolder.itemHome1Id.setText(batno);
//        viewHolder.itemHome1Name.setText((String) listItems.get(position).get("title"));
//        viewHolder.itemHome1Area.setText((String) listItems.get(position).get("region"));
//        viewHolder.itemHome1Count.setText((String) listItems.get(position).get("number"));
//        String status = (String) listItems.get(position).get("status");
//        if (status.equals(Constant.BATCH_FINAL)) {
//            viewHolder.itemHome1State.setText("已完成");
//            viewHolder.itemHome1State.setTextColor(Color.parseColor("#999999"));
//
//            viewHolder.itemHome1StateImg.setVisibility(View.VISIBLE);
//            viewHolder.itemHome1Button1.setVisibility(View.GONE);
//            viewHolder.itemHome1Button2.setVisibility(View.GONE);
//
//        } else {
//            viewHolder.itemHome1State.setText("未开始");
//            viewHolder.itemHome1State.setTextColor(Color.parseColor("#f04e53"));
//            viewHolder.itemHome1StateImg.setVisibility(View.GONE);
//            viewHolder.itemHome1Button1.setVisibility(View.VISIBLE);
//            viewHolder.itemHome1Button2.setVisibility(View.GONE);
//            viewHolder.itemHome1.setBackgroundColor(Color.parseColor("#fef3f2"));
//        }
//        if (!TextUtils.isEmpty(batInfo)) {
//            if (batInfo.equals(batno)) {
//                viewHolder.itemHome1.setBackgroundColor(Color.parseColor("#f1fff3"));
//                viewHolder.itemHome1State.setText("进行中");
//                viewHolder.itemHome1State.setTextColor(Color.parseColor("#40b542"));
//                viewHolder.itemHome1StateImg.setVisibility(View.GONE);
//                viewHolder.itemHome1Button1.setVisibility(View.VISIBLE);
//                viewHolder.itemHome1Button2.setVisibility(View.GONE);
//            }
//        }
//        viewHolder.itemHome1Date.setText((String) listItems.get(position).get("createTime"));
//        viewHolder.itemHome1Test.setOnClickListener(arg0 -> {
//            // TODO Auto-generated method stub
//            if (viewClickTest != null) {
//
//                viewClickTest.onClick(position);
//            }
//        });
//        viewHolder.itemHome1Config.setOnClickListener(arg0 -> {
//            // TODO Auto-generated method stub
//            if (viewClickConfig != null) {
//
//                viewClickConfig.onClick(position);
//            }
//        });
//        viewHolder.itemHome1Deldete.setOnClickListener(arg0 -> {
//            // TODO Auto-generated method stub
//            if (viewClickDelete != null) {
//
//                viewClickDelete.onClick(position);
//            }
//        });
//        viewHolder.itemHome1View.setOnClickListener(arg0 -> {
//            // TODO Auto-generated method stub
//            if (viewClickView != null) {
//
//                viewClickView.onClick(position);
//            }
//        });
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(@NonNull View v) {
        if (mOnItemLongClickListener != null) {
            mOnItemLongClickListener.onItemLongClick((String) v.getTag());
        }
        return true;
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }

    public interface OnRecyclerViewItemLongClickListener {
        void onItemLongClick(String s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}